package com.suiwei.frame;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suiwei.mybatisPlus.dao.BookDao;
import com.suiwei.mybatisPlus.entity.Book;
import com.suiwei.base.utils.DateUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @author suiwei
 * @date 2022-10-25 11:40
 */
@SpringBootTest(classes = MybatisPlus.class)
public class MybatisPlus {
    @Autowired
    BookDao bookDao;
    @Test
    void  test(){
        //查询
        bookDao.selectById(1);
        //查询全部
        bookDao.selectList(null);
        //分页查询，mybatis-plus的分页需要配置拦截器
        IPage page = new Page(1,2);
        bookDao.selectPage(page, null);
        page.getRecords();
        //按条件查询1
        QueryWrapper<Book> qw = new QueryWrapper<>();
        qw.like("name","书");
        bookDao.selectList(qw);
        //按条件查询2,可以判断是否为null，否则的话，null会被当做字符串，
        String name ="晓";
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
        lqw.like(name!=null,Book::getName,name);
        bookDao.selectList(lqw);


        //增加
        Book book = new Book();
        book.setDescription("数学书");
        book.setType("小说");
        //book.setName("围城");
        bookDao.insert(book);
        //修改
        book.setId(2);
        book.setName(DateUtils.getDate());
        bookDao.updateById(book);
        //删除
        bookDao.deleteById(3);
    }
}
